package com.shop.prj.dao.Impl;

import com.shop.prj.bean.Book;
import com.shop.prj.bean.BookStyle;
import com.shop.prj.bean.Feedback;
import com.shop.prj.dao.FeedbackDao;
import com.shop.prj.utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class FeedbackDaoImpl implements FeedbackDao {

	@Override
	public int insert(Feedback feedback) {
		String sql = "INSERT INTO `feedback`(`feedback_title`,`feedback_content`,`feedback_sent_time`,`feedback_sender`) VALUES (?,?,?,?)";
		int result = 0;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		connection = JDBCUtils.getConnection();
		try {
			preparedStatement = connection.prepareStatement(sql);

			preparedStatement.setString(1, feedback.getFeedbackTitle());
			preparedStatement.setString(2, feedback.getFeedbackContent());
			preparedStatement.setString(3, feedback.getFeedbackSentTime());
			preparedStatement.setInt(4, feedback.getFeedbackSender());

			result = preparedStatement.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				preparedStatement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();

			}
		}
		return result;
	}

	@Override
	public int update(Feedback feedback) {
		String sql = "UPDATE `feedback` SET `feedback_title`=?,`feedback_content`=?,`feedback_sent_time`=?,`feedback_sender`=? WHERE  `feedback_id`=?";
		int result = 0;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		connection = JDBCUtils.getConnection();
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, feedback.getFeedbackTitle());
			preparedStatement.setString(2, feedback.getFeedbackContent());
			preparedStatement.setString(3, feedback.getFeedbackSentTime());
			preparedStatement.setInt(4, feedback.getFeedbackId());
			result = preparedStatement.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				preparedStatement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();

			}
		}
		return result;
	}

	@Override
	public int delete(Long id) {
		String sql = "DELETE FROM `feedback` WHERE `feedback_id` = ?";
		Connection connection = JDBCUtils.getConnection();
		PreparedStatement preparedStatement = null;
		int result = 0;
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setLong(1, id);
			result = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				preparedStatement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	@Override
	public Feedback queryByFeedbackId(Long id) {
		String sql = "SELECT * FROM `feedback` WHERE `feedback_id`=?";

		Connection connection = JDBCUtils.getConnection();
		PreparedStatement preparedStatement = null;

		ResultSet resultSet = null;
		Feedback feedback = null;

		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setLong(1, id);
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				feedback = new Feedback();
				feedback.setFeedbackId(resultSet.getInt(1));
				feedback.setFeedbackTitle(resultSet.getString(2));
				feedback.setFeedbackContent(resultSet.getString(3));
				feedback.setFeedbackSentTime(resultSet.getString(4));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				preparedStatement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}

		return feedback;
	}

	@Override
	public List<Feedback> queryAll() {
		String sql = "SELECT * FROM `feedback`";

		Connection connection = JDBCUtils.getConnection();
		Statement statement = null;
		ResultSet resultSet = null;
		List<Feedback> list = new ArrayList<Feedback>();
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {

				Feedback feedback = new Feedback();
				feedback.setFeedbackId(resultSet.getInt(1));
				feedback.setFeedbackTitle(resultSet.getString(2));
				feedback.setFeedbackContent(resultSet.getString(3));
				feedback.setFeedbackSentTime(resultSet.getString(4));
				feedback.setFeedbackSender(resultSet.getInt(5));
				list.add(feedback);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				statement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public List<Feedback> queryByFeedbackSender(Long userid) {
		return null;
	}

	@Override
	public List<Feedback> queryByFeedback(String key) {
		String sql = "SELECT * FROM `feedback`WHERE `feedback_title` LIKE ? OR `feedback_content` LIKE ?";

		Connection connection = JDBCUtils.getConnection();
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<Feedback> list = new ArrayList<Feedback>();
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "%" + key + "%");
			preparedStatement.setString(2, "%" + key + "%");

			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Feedback feedback = new Feedback();
				feedback.setFeedbackId(resultSet.getInt(1));
				feedback.setFeedbackTitle(resultSet.getString(2));
				feedback.setFeedbackContent(resultSet.getString(3));
				feedback.setFeedbackSentTime(resultSet.getString(4));
				feedback.setFeedbackSender(resultSet.getInt(5));

				list.add(feedback);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				preparedStatement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
}
